package com.example.administrator.nurse.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.nurse.NetWork.RequestCallback;
import com.example.administrator.nurse.NetWork.RequestManager;
import com.example.administrator.nurse.NetWork.internetHelper;
import com.example.administrator.nurse.common;
import com.example.administrator.nurse.util.LoadingDialog;
import com.loopj.android.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/4/26.
 */
public abstract class BaseFragment extends Fragment  implements RequestCallback {

    private LoadingDialog loadingDialog;
    protected AppCompatActivity activity;
    private int layoutId;
    private View rootView;
    public String tag;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.rootView=inflater.inflate(setLayoutId(),container,false);
        initView(rootView);
        return rootView;
    }

    public String getJSONValue(JSONObject jsonObject,String key) throws JSONException {
        return jsonObject.isNull(key)?"移动护士":jsonObject.getString(key);
    }

    public int getJSONINTValue(JSONObject jsonObject,String key) throws JSONException {
        return jsonObject.isNull(key)?0:jsonObject.getInt(key);
    }

    public void t(String str){
        Toast.makeText(activity, str, Toast.LENGTH_SHORT).show();
    }

    public void t(String str,int time){
        Toast.makeText(activity, str, time).show();
    }

    protected abstract void initView(View rootView);
    public abstract int setLayoutId();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.activity= (AppCompatActivity) context;
    }

    public void startOtherActivity(Activity activity,Class<?> cls){
        if(activity!=null)
        startActivity(new Intent(activity,cls));
    }

    protected View f(int viewId){
        return rootView.findViewById(viewId);
    }

    protected abstract void onFailure(int requestCode,Object object, String errorMsg);

    protected abstract void onSuccess(int requestCode,Object object, String response);

    @Override
    public void onFailureCallback(int requestCode, Object object, String errorMsg) {
        onFailure(requestCode,object,errorMsg);
    }

    @Override
    public void onSuccessCallback(int requestCode, Object object, String response) {
        onSuccess(requestCode, object, response);
    }

    public void startLoadingDialog() {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog.createDialog(activity);
            //loadingDialog.show();
        }
        loadingDialog.show();
    }

    public void stopLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
        loadingDialog = null;
    }

    public void doGet(String url) {
        doGet(-1, url);
    }

    public void doGet(String url, RequestParams params) {
        doGet(-1, url, params);
    }

    public void doGet(int requestCode, String url) {
        doGet(requestCode, url, null);
    }

    public void doGet(int requestCode, String url, RequestParams params) {
        if (TextUtils.isEmpty(url)) {
            throw new RuntimeException("");
        }
        if(internetHelper.checkConnection()){
            RequestManager.get(requestCode, null, url, params, this);
        }else {
            common.Toast("无法连接网络!");
        }

    }

    public void doPost(String url, RequestParams params) {
        doPost(-1,null, url, params);
    }

    public void doPost(int requestCode,Object object, String url, RequestParams params) {
        if (TextUtils.isEmpty(url)) {
            throw new RuntimeException("");
        }
        if(internetHelper.checkConnection()){
            RequestManager.post(requestCode,object, url, params, this);
        }else {
            common.Toast("无法连接网络!");
        }
    }
}
